//
//  SelectTableView.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/8/15.
//  Copyright © 2016年 www.hd123.com. All rights reserved.
//

import UIKit

class SelectTableView: UIImageView , UIPickerViewDataSource , UIPickerViewDelegate{

     //MARK: - lifeCycle
    var dataArray: [[String : String]]
    var closeSure: (([String : String]) -> ())?
    var resultRow: Int = 0
    lazy private var pickerView:UIPickerView = {
        let picker = UIPickerView(frame: CGRect(x: 0, y: 0, width: KscreenWidth, height: 207))
        picker.backgroundColor = UIColor.whiteColor()
        return picker
    }()
    lazy private var sepectorView:UIView = {
        let sepectorView = UIView(frame: CGRect(x: 0, y: 83, width: KscreenWidth , height: 0))
        sepectorView.backgroundColor = UIColor.hexStringToUIColor("#979797")
        return sepectorView
    }()
    lazy private var mask:UIView = {
        let mask = UIView(frame: CGRect(x: 0, y: 0, width: KscreenWidth, height: KscreenHeight-64))
        mask.backgroundColor = UIColor.blackColor()
        mask.alpha = 0.5
        return mask
    }()

    init(dataArray: [[String : String]]){
        self.dataArray = dataArray
        super.init(frame: CGRect(x: 0, y: KscreenHeight - 207, width: KscreenWidth, height: 207))
        self.backgroundColor = UIColor.greenColor()
        self.pickerView.dataSource = self
        self.pickerView.delegate = self
        self.addSubview(pickerView)
        let lbutton = UIButton(frame: CGRect(x: 0, y:0, width: KscreenWidth/4+5, height: 40))
        lbutton.setTitle("取消", forState: UIControlState.Normal)
        lbutton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        lbutton.addTarget(self, action:  #selector(SelectTableView.pickerCacale(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        lbutton.backgroundColor = UIColor.hexStringToUIColor("#e5e5e5")
        self.addSubview(lbutton)
        
        let label = UILabel(frame: CGRect(x: KscreenWidth/4, y: 0, width: KscreenWidth/2, height: 40))
        label.backgroundColor = UIColor.hexStringToUIColor("#e5e5e5")
        label.text = "货品种类"
        label.textAlignment = NSTextAlignment.Center
        self.addSubview(label)
        
        let rbutton = UIButton(frame: CGRect(x: KscreenWidth - KscreenWidth/4, y:0, width: KscreenWidth/3, height: 40))
        rbutton.setTitle("确定", forState: UIControlState.Normal)
        rbutton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        rbutton.addTarget(self, action:  #selector(SelectTableView.pickerDone(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        rbutton.backgroundColor = UIColor.hexStringToUIColor("#e5e5e5")
        self.addSubview(rbutton)
        
        self.image =  UIImage(named: "")
        self.backgroundColor = UIColor.clearColor()
        self.addSubview(sepectorView)
        self.userInteractionEnabled = true
//        self.center = CGPointMake(KscreenWidth / 2, (KscreenHeight - 64) / 2)
//        self.bounds = CGRectMake(0, 0, KscreenWidth / 2, 44 * CGFloat(dataArray.count))
    }
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        if let tf = viewCacheManager.activedTextField() {
            tf.resignFirstResponder()
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func pickerDone(picker : UIPickerView){
        let dict = dataArray[resultRow]
        self.closeSure!(dict)
        self.removeFromSuperview()
        mask.removeFromSuperview()
        if let tf = viewCacheManager.activedTextField() {
            tf.becomeFirstResponder()
        }
    }
    func pickerCacale(picker : UIPickerView){
        self.removeFromSuperview()
        mask.removeFromSuperview()
        if let tf = viewCacheManager.activedTextField() {
            tf.becomeFirstResponder()
        }
    }
    //MARK: - tableView delegate
//    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//        let dict = dataArray[indexPath.row]
//        self.closeSure!(dict)
//        self.removeFromSuperview()
//    }
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        resultRow = row
    }
    func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 44
    }
    //MARK: - tableView DataSource
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return dataArray.count 
    }
//    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCellWithIdentifier("cell")
//        var title = self.dataArray[indexPath.row]["内码"]
//        if strlen(title ?? "") < 1 {
//            title = self.dataArray[indexPath.row]["货品内码"]
//        }
//        cell?.textLabel?.text = title
//        return cell!
//    }
//    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//        switch component {
//        case 0:
//            let title = self.dataArray[row]["名称"]
//            return title
//        case 1:
//            var title = self.dataArray[row]["内码"]
//            if strlen(title ?? "") < 1 {
//                title = self.dataArray[row]["货品内码"]
//            }
//            return title
//        default:
//            return ""
//        }
//    }
    func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
        var textRight = self.dataArray[row]["名称"]
        if strlen(textRight ?? "") < 1 {
            textRight = self.dataArray[row]["货品"]
        }
        var textLeft = self.dataArray[row]["货主"]
        if strlen(textLeft ?? "") < 1 {
            textLeft = self.dataArray[row]["货主"]
        }
        
        return label2View(textLeft: textLeft ?? "", textRight: textRight ?? "")
    }
    func showSelector(onView:UIView, success:([String : String]) -> ()){
        let window = UIApplication.sharedApplication().keyWindow
        window!.addSubview(mask)
        window!.addSubview(self)
        self.closeSure = success
    }
    func showSelectorHD(onView:UIView, success:([String : String]) -> ()){
        let window = UIApplication.sharedApplication().keyWindow
        window!.addSubview(mask)
        window!.addSubview(self)
        self.closeSure = success
    }

}
//MARK: - 用来展示picker中的view
class label2View: UIView {
    lazy private var labelLeft:UILabel = {
        let label = UILabel()
        label.textColor = UIColor.blackColor()
<<<<<<< HEAD
        label.font = UIFont.systemFontOfSize(15)
=======
//        label.backgroundColor = UIColor.greenColor()
        label.font = UIFont.systemFontOfSize(18)
>>>>>>> master
        return label
    }()
    lazy private var labelRight:UILabel = {
        let label = UILabel()
        label.textColor = UIColor.blackColor()
<<<<<<< HEAD
        label.font = UIFont.systemFontOfSize(15)
=======
//        label.backgroundColor = UIColor.redColor()
        label.font = UIFont.systemFontOfSize(19)
>>>>>>> master
        return label
    }()
    
    init(textLeft: String, textRight:String) {
        super.init(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
        labelLeft.text = textLeft
//        labelLeft.backgroundColor = UIColor.blueColor()
//        labelRight.backgroundColor = UIColor.greenColor()
        labelRight.text = textRight
        self.addSubview(labelLeft)
        self.addSubview(labelRight)
        labelLeft.snp_makeConstraints { (make) in
            make.centerX.equalTo(self).offset(40)
            make.centerY.equalTo(self)
        }
        labelRight.snp_makeConstraints { (make) in
            make.centerX.equalTo(self).offset(-40)
            make.centerY.equalTo(self)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
